package com.logstep;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
public class DrawView extends View {
    private final Paint mPaint;
    private final Paint mStrokePaint;
    private final Path mArrowPath;
    private final int arrowRadius = 20;
    private float mCurX = 0;
    private float mCurY = 0;
    private int mOrient;
    private final List<PointF> mPointList = new ArrayList<>();
    private boolean isClear=false;

//    Location location;
    public DrawView(Context context) {
        this(context, null);
    }
    public DrawView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mStrokePaint = new Paint(mPaint);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(5);
        mArrowPath = new Path();
        mArrowPath.arcTo(new RectF(-arrowRadius, -arrowRadius, arrowRadius, arrowRadius), 0, -180);
        mArrowPath.lineTo(0, -3 * arrowRadius);
        mArrowPath.close();


//        mCurX=getWidth()/2;
//        mCurY=getHeight()/2;
    }
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        if (isClear){
            canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
            mPointList.clear();
            isClear=false;
        }else{
            for (PointF p : mPointList) {
                canvas.drawCircle(p.x, p.y, 10, mPaint);
            }

            //画网格
            if (mCurX==mCurX && mCurX==0){
                mCurX=getWidth()/2;
                mCurY=getHeight()/2;
            }

            int w = getWidth() / 2;
            int h=getHeight()/2;

            mPaint.setColor(Color.BLACK);
            canvas.drawLine(0,h,getWidth(),h,mPaint);
            canvas.drawLine(w,0,w,getHeight(),mPaint);
            mPaint.setColor(Color.GRAY);

            int emmm=0;//用来记录循环了几次
            //上面的线
            for (int i=h-50;i>5;i=i-50){
                emmm++;
                canvas.drawLine(0,i,getWidth(),i,mPaint);
                canvas.drawText(String.valueOf(emmm),w,i,mPaint);
            }
            emmm=0;
            //下面的线
            for (int i=h+50;i<getHeight();i=i+50){
                emmm--;
                canvas.drawLine(0,i,getWidth(),i,mPaint);
                canvas.drawText(String.valueOf(emmm),w,i,mPaint);
            }
            emmm=0;
            //左边的线
            for (int i=w-50;i>5;i=i-50){
                emmm--;
                canvas.drawLine(i,0,i,getHeight(),mPaint);
                canvas.drawText(String.valueOf(emmm),i,h,mPaint);
            }
            //右边的线
            emmm=0;
            for (int i=w+50;i<getWidth();i=i+50){
                emmm++;
                canvas.drawLine(i,0,i,getHeight(),mPaint);
                canvas.drawText(String.valueOf(emmm),i,h,mPaint);
            }
            mPaint.setColor(Color.BLUE);

            canvas.save();
            canvas.translate(mCurX, mCurY);
            canvas.rotate(mOrient);
            canvas.drawPath(mArrowPath, mPaint);
            canvas.drawArc(new RectF(-arrowRadius * 0.8f, -arrowRadius * 0.8f, arrowRadius * 0.8f, arrowRadius * 0.8f), 0, 360, false, mStrokePaint);
            canvas.restore();
        }
    }
    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mCurX = event.getX();
        mCurY = event.getY();
        invalidate();
        return true;
    }
    public void autoAddPoint(float stepLen,Location location) {
        mCurX += (float) (stepLen * Math.sin(Math.toRadians(mOrient)));
        mCurY += -(float) (stepLen * Math.cos(Math.toRadians(mOrient)));
        //计算当前位置

        float nowX;
        if (mCurX>getWidth()/2){
            nowX =(mCurX-getWidth()/2)/50;
        }else if (mCurX<getWidth()/2){
            nowX =-(getWidth()/2-mCurX)/50;
        }else {
            nowX =0;
        }

        float nowY;
        if (mCurY>getHeight()/2){
            nowY =-(mCurY-getHeight()/2)/50;
        }else if (mCurY<getHeight()/2){
            nowY =(getHeight()/2-mCurY)/50;
        }else {
            nowY =0;
        }
        location.location(nowX, nowY);
//        Log.i("emmmmm","("+nowX+","+nowY+")");
        mPointList.add(new PointF(mCurX, mCurY));
        invalidate();
    }
    public void autoDrawArrow(int orient) {
        mOrient = orient;
        invalidate();
    }
    public void clear(){
        isClear=true;
        invalidate();
    }
}